// gulp的入口文件

// 导出函数成员的方式进行定义

// exports.foo = () => {
//   console.log('foo task working');
// }

// Starting 'foo'...
// foo task working
// The following tasks did not complete: foo
// Did you forget to signal async completion?
// error Command failed with exit code 1.


exports.foo = done => {
  console.log('foo task working');
  done() // 标识任务完成
}

// default 会作为gulp的默认任务出现。我们在运行时，不需要指定直接可运行gulp
exports.default = done => {
  console.log('default task working');
  done()
}


const gulp = require('gulp')
// 4.0以前需要使用gulp里面提供的方法来注册任务
// 可以通过gulp.task来注册一个任务
// 不在推荐使用这种方法创建任务，
// 推荐使用导出函数成员的方式去定义gulp任务
gulp.task('bar', done => {
  console.log('bar working');
  done()
})

const { series, parallel} = require('gulp')
const task1 = done => {
  setTimeout(() => {
    console.log('task1 working');
    done()
  }, 1000);
}

const task2 = done => {
  setTimeout(() => {
    console.log('task2 working');
    done()
  }, 1000);
}

const task3 = done => {
  setTimeout(() => {
    console.log('task3 working');
    done()
  }, 1000);
}

// 串行任务结构
exports.baz = series(task1, task2, task3)

// 并行任务结构
exports.bar = parallel(task1, task2, task3)


// 异步任务方法: 回调
exports.callback = done => {
  console.log('callback task');
  done()
}
exports.callback_error = done => {
  console.log('callback task');
  done(new Error('task failed'))
}

exports.promise = () => {
  console.log('promise task');
  return Promise.resolve()
}
exports.promise_error = () => {
  console.log('promise task');
  return Promise.reject(new Error('task failed'))
}

const timeout = time => {
  return new Promise(resolve => {
    setTimeout(resolve, time);
  })
}
exports.async = async () => {
  await timeout(1000)
  console.log('async task');
}


const fs = require('fs')
exports.stream = () => {
  const readStream = fs.createReadStream('package.json')
  const writeStream = fs.createWriteStream('temp.text')
  readStream.pipe(writeStream)
  readStream.on('end', () => {
    done()
  })
}